<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<h1>发布微博</h1>
<div id="app">
    <input type="text" v-model="weibo.content" placeholder="说点儿啥..">
    <el-upload
            action="/upload"
            name="picFile"
            :limit="1"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <input type="button" value="发布" @click="insert()">

</div>
</body>
<!-- import Vue before Element -->
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{
                    content:"",
                    url:""
                }
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                console.log(file.response)
                axios.get("/remove?name="+file.response).then(function (response) {
                    console.log("服务器图片已删除")
                })
            },

            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response,file,fileList){
                console.log("文件上床成功,图片名="+response);
                v.weibo.url=response;

            },
            insert(){
                if (v.weibo.content.trim()==""||v.weibo.url==""){
                    alert("上床内容不能为空!")
                    return;
                }
                axios.post("/insert",v.weibo).then(function (response) {
                    if (response.data==1){
                        alert("添加完成!")
                        location.href="/"
                    }else {
                        alert("请先登录!")
                        location.href="/login.html"
                    }

                })
            }
        }
    })
</script>
</html>